package com.eiei.thread;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.eiei.manager.MessageManager;
import com.eiei.utils.ConsoleUtils;

/**
 * 接受邮件线程默认，30秒接受一次
 * @author eiei
 *
 */
public class MessageReciveThread extends BaseThread {

	private int interval = 30;
	
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public int getInterval() {
		return interval;
	}

	public void setInterval(int interval) {
		this.interval = interval;
	}

	private static MessageManager manager = new MessageManager();
	
	public MessageReciveThread(){
		ThreadContext.reciveThread = this;
	}
	
	public MessageReciveThread(int interval){
		ThreadContext.reciveThread = this;
		this.interval = interval;
	}

	@Override
	public void run() {
		runStat = RUNNING;
		while(BaseThread.DIE_OUT != runStat && interval>0){
			if(RUNNING == runStat){
				int count = manager.reviceMessages();
//				if(count>0){
//					ConsoleUtils.output("["+sdf.format(new Date())+"]收到了"+count+"条信息。");
//				}
			}
			threadSleep(interval);
		}
		System.out.println("接收线程死亡。");
	}
	
	public static void startThread(){
		ExecutorService threadPool = Executors.newSingleThreadExecutor();
		threadPool.execute(new MessageReciveThread());
	}
	
}
